package ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Bean.AchievementBean;
import base.BaseFra;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.profit.biz.AchievementFragmentBiz;
import ui.activity.profit.component.DaggerAchievementFragmentComponent;
import ui.activity.profit.contract.AchievementFragmentContract;
import ui.activity.profit.module.AchievementFragmentModule;
import ui.activity.profit.presenter.AchievementFragmentPresenter;
import ui.adapter.AchievementAdapter;
import util.AlertUtils;
import util.SysUtils;

/**
 * 业绩页面
 */
public class AchievementFra extends BaseFra implements AchievementFragmentContract.View {

    @Inject
    AchievementFragmentPresenter presenter;
    @Inject
    AchievementFragmentBiz biz;
    @BindView(R.id.profit_day)
    TextView day;
    @BindView(R.id.profit_check)
    TextView check;
    @BindView(R.id.perform_ren)
    TextView renNumber;
    @BindView(R.id.perform_shop)
    TextView shopNumber;
    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    private AchievementAdapter adapter;

    private String selectYear = "";
    private String SelectMouth = "";

    private String year = "";
    private String mouth = "";

    private String lastYear = "";
    private String lastMouth = "";

    private String lastPlusYear = "";
    private String lastPlusMouth = "";
    long selectTime;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_achievement, container, false);
        ButterKnife.bind(this, view);
        DaggerAchievementFragmentComponent.builder().achievementFragmentModule(new AchievementFragmentModule(this)).build().inject(this);
        selectTime=System.currentTimeMillis();
        presenter.setBiz(biz);
        day.setText(SysUtils.sf3.format(new Date(System.currentTimeMillis())));
        Calendar mLastMouth = Calendar.getInstance();
        mLastMouth.add(Calendar.MONTH, -1);
        Date mLast = mLastMouth.getTime();

        Calendar mLastPlusMouth = Calendar.getInstance();
        mLastPlusMouth.add(Calendar.MONTH, -2);
        Date mLastPlus = mLastPlusMouth.getTime();


        String[] strDate = SysUtils.sf2.format(new Date(System.currentTimeMillis())).split("-");
        if (strDate.length == 2) {
            year = strDate[0];
            mouth = strDate[1];
            selectYear = strDate[0];
            SelectMouth = strDate[1];
        }

        String[] monthLast = SysUtils.sf2.format(mLast).split("-");
        if (monthLast.length == 2) {
            lastYear = monthLast[0];
            lastMouth = monthLast[1];
        }

        String[] mouthPlus = SysUtils.sf2.format(mLastPlus).split("-");
        if (mouthPlus.length == 2) {
            lastPlusYear = mouthPlus[0];
            lastPlusMouth = mouthPlus[1];
        }
        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getSearchInfo(true, selectYear, SelectMouth));
        RxView.clicks(day)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker3(getActivity(), 365 * 5, 0,selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime=millseconds;
                            day.setText(SysUtils.sf3.format(new Date(millseconds)));
                            String[] strDate = SysUtils.sf2.format(new Date(millseconds)).split("-");
                            if (strDate.length == 2) {
                                selectYear = strDate[0];
                                SelectMouth = strDate[1];
                            }
                        }
                    });
                    timePickerDialog.show(getActivity().getSupportFragmentManager(), "all");
                });
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getInfo();
                presenter.getInfo1(true, year, mouth);
                presenter.getInfo2(lastYear, lastMouth);
                presenter.getInfo3(lastPlusYear, lastPlusMouth);
            }
        });
        ptr.autoRefresh(true);
        return view;
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void upDateNumberUI(String allyNumber, String merchantNumber) {
        renNumber.setText(allyNumber);
        shopNumber.setText(merchantNumber);
    }

    @Override
    public void upDateUI(List<AchievementBean.DataBean.ItemsBean> bean) {
        if (adapter == null) {
            adapter = new AchievementAdapter(getActivity(), bean);
            rv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }
}
